var itemType = require('item.js');
var colorCal = require('color.js');
var collectColorCal = colorCal.collectColor;
var app = getApp();
(function(){
    var collectItem = itemType.createItem();

    collectItem.handleCollectData = function() {
        this.count = this.data.count - this.data.doneCount;
        if (this.count > 0) {
            this.textColor = 'rgba(255, 255, 255, 1)';
        }
        else {
            this.textColor = 'rgba(255, 255, 255, 0.5)';
        }
    };
    collectItem.updateColor = function(count) {
        this.sliderColor = collectColorCal.calItemColor(this.getOrder(), count);
        this.render('sliderColor', this.sliderColor);
    };
    collectItem.calColor = function(count) {
        this.sliderColor = collectColorCal.calItemColor(this.getOrder(), count);
    };
    collectItem.onTap = function(e) {
        if (e.target.id === 'text') {
            this.onEditStart();
        }
        else {
            this.open();
        }
    };
    collectItem.open = function() {
        this.list.open(this);
    };
    collectItem.complete = function() {
        if (this.count === 0) {
            return;
        }
        var confirmContent = '是否要将事项集合"' + this.getText() + '"中的全部事项标记完成?'
        var item = this;
        wx.showModal({
            title: '完成集合',
            content: confirmContent,
            success: function(res) {
                if (res.confirm) {
                    item.setCount(item.data.count, 0); 
                    item.list.doneItem(item);
                }
            }
        });
    };

    collectItem._del = function() {
        var item = this;
        this.disappear(function() {
            item.deleted = true;
            item.list.delItem(item);
        });
    };
    collectItem.del = function() {
        if (this.count === 0) {
            this._del();
            return;
        }

        var confirmContent = '是否要删除事项集合"' + this.getText() + '"?'
        var item = this;
        this.goback(function() {
            wx.showModal({
                title: '删除事项集合',
                content: confirmContent,
                success: function(res) {
                    if (res.confirm) {
                       item._del(); 
                    }
                }
            });
            //item._del();
        });
    };
    collectItem.setCount = function(totalCount, count) {
        this.data.count = totalCount;
        this.data.doneCount = totalCount - count;
        this.count = count;
        this.updateTextColor();
        this.render('count', count);
        this.list.save();
    };
    collectItem.updateTextColor = function() {
        if (this.count === 0) {
            this.setTextColor('rgba(255, 255, 255, 0.5)');
        }
        else {
            this.setTextColor('rgba(255, 255, 255, 1)');
        }
    };
    collectItem.setTextColor = function(textColor) {
        if (this.textColor !== textColor) {
            this.textColor = textColor;
            this.render('textColor', textColor);
        }
    };
    collectItem.touchmove = function(dx) {
        this.ontouchmove(dx);
    };
    collectItem.touchend = function() {
        this.ontouchend();
    };

    collectItem.setHeight = function(height) {
        this.height = height;
        this.render('height', height);
        this.render('collectHeight', height);
    };

    collectItem.setSortItemStyle = function() {
        var sortItem = this.list.sortItem;
        sortItem.setStyle(this.height, this.getText(), 
            this.textColor, this.sliderColor, this.count);
    };

    function createItem() {
        return Object.create(collectItem);
    }
    module.exports = {
        createItem:createItem
    }


})();